// ITI1120 (Winter 2012), Lab7
// Name: Gilbert Arbez, Student Number: 1234567
// Lab Group: LAB2 

import java.io.* ;

class Characters
{

  /*
   *  Method that counts the number of lowercase characters.
   * 
   *  
   * 
   * Test dans Interaction: 
   * System.out.println(Lab8Ex1.compteMinuscule("Bonjour"));
   */   
  public static int countLowercase(String str)
   {
     int index;    // INTERMEDIAIRIES: index for all str
     int counter; // RESULT: number of lowercase characters
     
     compteur=0; 

     for(index=0; index<str.length(); index = index+1)
     {
         if(str.charAt(index)>='a' && str.charAt(index)<='z')   // lowercase
         {
            counter = counter+1;
         }
         else
         {
            ; // Do nothing
         }     
     }
     return counter;
   }
   
  /* Method that transforms all lowercase characters to uppercase and vice versa. Uses the usual functions:
   *length(), substring(...), charAt(...), toUpperCase(...), toLowerCase(...) and concat(...)
   *GIVEN: str
   */   
  public static String renverseLowerUpper(String str)
   {
     int index;                   // INTERMEDIAIRIES: for the loop
     String tempChar;  // INTERMEDIARIES:
     String result = "";        // RESULT: 
     
      for(index=0; index<str.length(); index=index+1)   
      // attention, length() with parentheseses!
      {
         tempChar = str.substring(index, index+1);  
         // a String with a single character

         if(tempChar.charAt(0)>='a' &&      
            tempChar.charAt(0)<='z')   // lowercase
         {
            tempChar = tempChar.toUpperCase();
         }
         else
         {
           if(tempChar.charAt(0)>='A' && 
              tempChar.charAt(0)<='Z')  // uppercase
           {
              tempChar = tempChar.toLowerCase();
           }
           else
           {
              ; // do nothing, not a letter
           }   // end of uppercase IF
         }  // end of lowercase IF
  
         // concat all characters with the new result of the converted character
         result = result.concat(tempChar);  
   
      } // end of for loop
      
      return result;       
   }
  
  /* Method that transforms lowercase characters into uppercase ones and vice versa (version 2)
   *Method uses a char[] array and its length, along with the usual methods of String: length(), toCharArray()
   *The conversion is done according to the characters unicode value 
   *Ex: 'a' = 97, and 'A' = 65. To convert you would need to add or substract 32 ('a' - 'A')
   * 
   */ 
   public static String renverseMinusMajusVersion2(String str)
   {
     int index;                          // INTERMEDIARIES:
     char arrayStr[] = str.toCharArray();  // INTERMEDIARIES:
     String result;                      // RESULT:
     
     for(index=0; index<arrayStr.length; index=index+1) 
        // attention! length without paranthesis
     {
         if(arrayStr[index]>='a' && arrayStr[index]<='z')   // lowercase ?
         {
            arrayStr[index] = (char) (arrayStr[index]+'A'-'a');  // typecasting
         }
         else
         {
           if(arrayStr[index]>='A' && arrayStr[index]<='Z')  //uppercase ?
           {
              arrayStr[index] = (char) (arrayStr[index]+'a'-'A');  // typecasting
           }
           else
           {
              ; // Do nothing
           }   // end of uppercase IF
         }  // end of lowercase IF
      } // end of for loop     
     result = new String(arrayStr);  // Use String constructor
     
     return result;       
   }
  
}
